<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
		
if (!isset($_POST['is_submitted'])) {
	$_POST['is_submitted'] = '';
}
if (!isset($error_message)) {
	$error_message = '';
}	
$cat_id = $_REQUEST["page_id"];

if($_POST["redirect"] || $_SESSION['redirect'] ){
	if($_POST["redirect"]) $redirect = $_POST["redirect"];
	if($_SESSION['redirect']){ 
	$redirect = $_SESSION['redirect'];
	unset($_SESSION['redirect']);
	}
}else{
       $redirect = $_SERVER['HTTP_REFERER'];	
     }		
	
	if ($_POST['is_submitted']) {
		$error_message = '';
		$redirect = $_POST["redirect"];
    
	$sql =  'SELECT lang_id,Name,code FROM language WHERE published ';
	$result_lang = query($sql);
	
	while ($row_lang = mysql_fetch_object($result_lang)) {
	
	
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
			
				if (!$name)  $error_message .= 'Title is missing in '.$row_lang->Name.'<br/>';
	}
	
	if (!$_POST['sef_url'])  $error_message .= 'SEF URL is missing<br/>';
		elseif(!preg_match('/^[0-9A-Za-z\p{Cyrillic}\-\_]+$/u', $_POST['sef_url'])) $error_message .= 'SEF URL is invalid<br/>';
		
		
	$sql = "SELECT
				page_id
			FROM
				pages
			WHERE
				sef_url = '".$_POST['sef_url']."'
				AND 
				page_id != '$cat_id'
			  ";
	$result = query($sql);
	if ($row_seo = mysql_fetch_object($result)) $error_message .= 'SEF URL is in use<br/>';
	
	
	//ОК
    if(!$error_message) {
		
		//$upload_path = "../smallimg/";
			
		//Създаваме проекта
		$sql = "UPDATE pages SET
				  sef_url = '".escape($_POST['sef_url'])."'
				 
				 WHERE
				 page_id = '$cat_id'
				";
		query($sql);

		$sql = 'SELECT lang_id,Name,code
				FROM
				language
				WHERE
				published
					';
		$result_lang = query($sql);
		
		while ($row_lang = mysql_fetch_object($result_lang)) {
			
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			
			$sql = "UPDATE page_description SET
				  
				   title = '".escape($name)."', 
				   description = '".escape($description)."', 
				   meta_key = '".escape($meta_k)."',
				   meta_description = '".escape($meta_d)."'
				WHERE
					page_id = '$cat_id'
					AND
					lang_id = '$em'
				  ";
				 
		query($sql);
		}
		
		
		$_SESSION['message'] .= 'Success!';
         if($_POST["redirectForm"]){
           redirect($_POST["redirectForm"]);
		}else{
			$_SESSION['redirect'] = $redirect;
		   redirect($_SERVER['HTTP_SELF'].'?page_id='.$cat_id);
		}
    }

}

# Initial start
$sql = "SELECT 
			page_id,
			sef_url,
			published
			
		FROM
			pages
		WHERE
			page_id ='$cat_id'
		
		";
	$result_cat = query($sql);
	$row_cat = mysql_fetch_object($result_cat);
	
	$sef = $row_cat->sef_url;
	if ($_POST['is_submitted'])
	{
	$sef = $_POST['sef_url'];
	}
				


if (!$row_cat) redirect("pages.php");

	
	head_admin("Page edit");
?>

<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	language : "en", 
	mode : "exact",
    elements : "body1,body2,body3,body4,body5",
	theme : "advanced",
	relative_urls : false,
	height : 300,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,image,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,

	// Drop lists for link/image/media/template dialogs
	//template_external_list_url : "js/template_list.js",
	external_link_list_url : "js/link_list.php",
	//media_external_list_url : "js/media_list.js",
	//external_image_list_url : "js/blog_image_list.php"
	
});
</script>
		
            <?php if ($error_message) { ?>
			<div class="message red">
				<h3>Error!</h3>
				<?=$error_message?>
			</div>
			<?php } ?>
            
            <?php if ($_SESSION['message']) { ?>
    		<div class="message green">
       			 <?=$_SESSION['message']?>
    		</div>
			<?php unset($_SESSION['message']); } ?>
			
            <div class="topbuttons">
            	<a href="<?=$redirect?>">Back</a>
            </div>
			
            <h3>Page edit</h3>
			
            <form id="page-edit-id" action="<?=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="redirect" value="<?=$redirect?>" />
            <input type="hidden" name="page_id" value="<?=$row_cat->page_id?>" />
            <input id="redirectForm" type="hidden" name="redirectForm" value="" />
            <input type="hidden" name="redirect" value="<?=$redirect?>" />
            
              <dt><label class="required">SEF URL:</label>
        <?=HTML_BASE?><input id="sef_url"  name="sef_url" type="text" value="<?=$sef?>" >
        </dt>		
       				
			
<?php 
			
			$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
			$result = query($sql);
			
			$i=0;
			
			?>
         
                 
     	<div id="tabs">
        
			<ul id="tabs-ui">
			</ul>
			
			
            
			<?php
			$tabs="";
			while ($row_lang = mysql_fetch_object($result)) {
				$em = $row_lang->lang_id;
				
				$tabs.=' <li><a href="'.$_SERVER['PHP_SELF'].'?page_id='.$cat_id.'#tabs-'.$em.'">'.$row_lang->Name.'</a></li>';

				
					$sql_d = "SELECT *	
					FROM
						page_description
					WHERE
						page_id = '$cat_id'
						AND lang_id = '$em'
					
					";
					$result_cat_d = query($sql_d);
					$row_cat_d = mysql_fetch_object($result_cat_d);
					$name = $row_cat_d->title;
					$description = $row_cat_d->description;
					$meta_d = $row_cat_d->meta_description;
					$meta_k = $row_cat_d->meta_key;
					
					
			if ($_POST['is_submitted']) {
				
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			    
				
			}
			
								  
			?>
            	
		
              
            <div id="tabs-<?=$em?>">
               
              <dl class="clearfix">
                <h3>Data in  <?=$row_lang->Name?></h3>
                <dt><label class="required">Title in <?=$row_lang->Name?></label></dt>
                <dd><input class="inputbox w500" name="title_<?=$row_lang->lang_id?>" type="text" value="<?=$name?>" ></dd>
                
                <dt><label>Description in <?=$row_lang->Name?></label></dt>
                <dd><textarea id="body<?=$row_lang->lang_id?>"  class="inputbox" style="width:100%" name="description_<?=$row_lang->lang_id?>" rows="16"><?=sth($description)?></textarea></dd>
                
                
                <dt><label >Meta Description in <?=$row_lang->Name?>(Up to 110 symbols)</label></dt>
                <dd><textarea class="inputbox w500" name="meta_description_<?=$row_lang->lang_id?>" rows="3" maxlength="110"><?=$meta_d?></textarea></dd>
                
                <dt><label >Keywords  in <?=$row_lang->Name?>(Up to 110 symbols)</label></dt>
                <dd><textarea class="inputbox w500" name="meta_key_<?=$row_lang->lang_id?>" rows="3" maxlength="110"><?=$meta_k?></textarea></dd>
                
                            
              </dl>
            </div>
        
                        
            
		 <?php
				
			}
			
			
	  	  ?>
            		
			
       </div>
       
       		
			</form>  
            
            
            
   <script>
	$(document).ready(function() {
           $("#tabs-ui").html('<?=$tabs?>');
	       $( "#tabs" ).tabs();	  
      });		
  </script>
  
            
<?php
    controls($redirect, "#page-edit-id");
	foot_admin(); 
?>
